import {Component, OnInit} from '@angular/core';
import {PpypService} from '../../ppyp.service';
import {NzMessageService} from 'ng-zorro-antd/message';

enum STATUS {
  '启用',
  '禁用',
  '删除'
}

enum COMMODITYSTATUS {
  '无效',
  '已上架',
  '待上架'
}

enum USERTYPE {
  '通用',
  'C端',
  'B端',
}

@Component({
  selector: 'app-ppyp-category',
  templateUrl: './ppyp-category.component.html',
  styleUrls: ['./ppyp-category.component.scss']
})
export class PpypCategoryComponent implements OnInit {
  STATUS = STATUS;
  COMMODITYSTATUS = COMMODITYSTATUS;
  USERTYPE = USERTYPE;
  // 表格数据
  listOfData = {
    page: 1,
    total: 100,
    records: []
  };
  tableQuery = {
    page: 1,
    pageSize: 9999
  };
  addCategoryModal = {
    visible: false,
    id: '',
    name: '',
    image: '',
    status: 0
  };
  commodityModal = {
    visible: false,
    type: 'add',
    typeOptions: [],
    brandOptions: [],
    listOfData: {
      page: 1,
      total: 100,
      records: []
    },
    query: {
      goods: '',
      typeList: [],
      brandList: [],
      isAdd: false,
      goodsType: '',
      page: 1,
      pageSize: 10,
    }
  };

  constructor(
    public ppypService: PpypService,
    private nzMessageService: NzMessageService
  ) {
  }

  ngOnInit(): void {
    this.getCommodity();
  }

  add() {
    this.addCategoryModal.visible = true;
  }

  hideAddCategoryModal() {
    this.addCategoryModal = {
      visible: false,
      id: '',
      name: '',
      image: '',
      status: 0
    };
  }


  editCategory(data: any) {
    this.addCategoryModal = {
      visible: true,
      id: data.id,
      name: data.name,
      image: data.icon,
      status: data.status
    };
  }

  saveCategory() {
    if (!this.addCategoryModal.name) {
      this.nzMessageService.warning('请输入分类名称');
      return;
    }
    if (!this.addCategoryModal.image) {
      this.nzMessageService.warning('请选择分类图片');
      return;
    }
    this.ppypService.categoryAddAndModify({
      name: this.addCategoryModal.name,
      icon: this.addCategoryModal.image,
      status: this.addCategoryModal.status,
      id: this.addCategoryModal.id
    }).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.hideAddCategoryModal();
        this.getCommodity();
      }
    });
  }

  hideCommodityModal() {
    this.commodityModal = {
      visible: false,
      type: 'add',
      typeOptions: [],
      brandOptions: [],
      listOfData: {
        page: 1,
        total: 100,
        records: []
      },
      query: {
        goods: '',
        typeList: [],
        brandList: [],
        isAdd: false,
        goodsType: '',
        page: 1,
        pageSize: 10,
      }
    };
    this.getCommodity();
  }

  saveCommodity() {
    console.log(this.addCategoryModal);
  }

  tableQueryBtn() {
    this.tableQuery.page = 1;
    this.getCommodity();
  }


  getCommodity() {
    this.ppypService.getCategoryList(this.tableQuery).then((res: any) => {
      if (res.code === 0) {
        this.listOfData.records = res.data.records;
      }
    });
  }

  onPageIndexChange(index: number) {
    this.commodityModal.query.page = index;
    this.changeView();
  }

  onPageSizeChange(pageSize: number) {
    this.commodityModal.query.pageSize = pageSize;
    this.changeView();
  }

  queryBtn() {
    this.commodityModal.query.page = 1;
    this.changeView();
  }

  resetCondition() {
    this.commodityModal.query.goods = '';
    this.commodityModal.query.typeList = [];
    this.commodityModal.query.brandList = [];
    this.commodityModal.query.isAdd = false;
    this.commodityModal.query.page = 1;
    this.commodityModal.query.pageSize = 10;
    this.queryBtn();
  }

  showCommodityModal(id: any) {
    this.commodityModal.visible = true;
    this.commodityModal.query.goodsType = id;
    this.ppypService.categoryCommodityType().then((res: any) => {
      if (res.code === 0) {
        this.commodityModal.typeOptions = this.addFieldArr(res.data);
      }
    });
    this.ppypService.categoryCommodityBrandList().then((res: any) => {
      if (res.code === 0) {
        this.commodityModal.brandOptions = res.data;
      }
    });
    this.changeView();
  }

  // 商品分类下拉数据处理
  addFieldArr(arr: any, key?: any, value: any = null, level: any = 0) {
    return arr.map((item: any) => {
      if (item.children && item.children.length > 0) {
        item.disabled = true;
        item.children = this.addFieldArr(item.children);
      }

      if (item.children && item.children.length <= 0) {
        item.isLeaf = true;
      }

      return {
        ...item,
        key: item.id,
        title: item.name
      };
    });
  }

  changeView(reset = false) {
    this.commodityModal.query.isAdd = this.commodityModal.type === 'remove';
    if (reset) {
      this.commodityModal.query.page = 1;
    }
    this.commodityModal.listOfData.records = [];
    this.ppypService.getCategoryCommodity(this.commodityModal.query).then((res: any) => {
      if (res.code === 0) {
        console.log(res.data);
        this.commodityModal.listOfData = res.data;
      }
    });
  }

  move(id: any, type: 0 | 1) {
    this.ppypService.categoryMove(id, type).then((res: any) => {
      console.log(res);
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.tableQueryBtn();
      }
    });
  }

  // 0禁用 1启用 2删除
  changeCategoryStatus(data: any, status: 0 | 1 | 2) {
    this.ppypService.categoryAddAndModify({
      name: data.name,
      icon: data.image,
      status,
      id: data.id
    }).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.tableQueryBtn();
      }
    });
  }

  changeStatus(data: any, isSave: boolean) {
    this.ppypService.categoryCommodityAdd({
      goodsType: this.commodityModal.query.goodsType,
      isSave,
      id: data.id,
      salesVolume: data.salesVolume,
      shopPriceC: data.shopPriceC,
      salesVolumeBase: data.salesVolumeBase,
      status: data.status,
      updateTime: data.updateTime,
      userType: data.userType,
      stockNum: data.stockNum,
    }).then((res: any) => {
      if (res.code === 0) {
        this.changeView();
        this.tableQueryBtn();
      }
    });
  }
}
